<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div v-loading="loading" class="edit-form">
        <el-form ref="userInfo" label-width="auto" :model="userInfo" :rules="rules" status-icon>
          <!-- 姓名 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="userInfo.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 工号 入职时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号" prop="workNumber">
                <!-- 系统自动生产 不允许修改 -->
                <el-input :value="userInfo.workNumber" disabled size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机 聘用形式  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input v-model="userInfo.mobile" size="mini" class="inputW" :disabled="$route.params.id ? true : false" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置及联部门组件 封装-->
                <selectTree v-model="userInfo.departmentId" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 聘用形式 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select v-model="userInfo.formOfEmployment" size="mini" class="inputW">
                  <el-option label="正式" :value="1" />
                  <el-option label="非正式" :value="2" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 入职时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="userInfo.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 转正时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="userInfo.correctionTime"
                  size="mini"
                  type="date"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- 放置上传图片 -->
                <imageUpload v-model="userInfo.staffPhoto" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12">
              <el-form-item>
                <el-button size="mini" type="primary" @click="saveData">
                  {{ $route.params.id ? '保存更新' : '新增员工' }}
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

    </div>
  </div>
</template>

<script>
import selectTree from './components/select-tree.vue'
import imageUpload from './components/image-upload.vue'
import { addEmployee, getEmployeeInfo, uploadEmployeeInfo } from '@/api/employee.js'
export default {
  name: 'EmployeeDetail',
  components: {
    selectTree,
    imageUpload
  },
  data() {
    return {
      loading: false,
      userInfo: {
        username: '', // 用户名
        mobile: '', // 手机号
        workNumber: '', // 工号
        formOfEmployment: null, // 聘用形式
        departmentId: null, // 部门id
        timeOfEntry: '', // 入职时间
        correctionTime: '', // 转正时间
        staffPhoto: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 4, message: '姓名为1-4位', trigger: 'blur' }
        ], // 用户名
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            //   pattern 正则表达式
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }], // 手机号
        formOfEmployment: [{ required: true, message: '请选择聘用形式', trigger: 'blur' }], // 聘用形式
        departmentId: [{ required: true, message: '请选择部门', trigger: 'blur' }], // 部门id
        timeOfEntry: [{ required: true, message: '请选择入职时间', trigger: 'blur' }], // 入职时间
        correctionTime: [
          { required: true, message: '请选择转正时间', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (this.userInfo.timeOfEntry) {
                if (new Date(this.userInfo.timeOfEntry) > new Date(value)) {
                  callback(new Error('转正时间不能小于入职时间'))
                  return
                }
              }
              callback()
            }
          }]// 转正时间
      }
    }
  },
  created() {
    // 如果有id 则执行获取员工基本信息
    this.$route.params.id && this.GetEmployeeInfo()
  },
  methods: {
    // 获取员工基本信息
    async GetEmployeeInfo() {
      this.loading = true
      this.userInfo = await getEmployeeInfo(this.$route.params.id)
      console.log(this.userInfo)
      this.loading = false
    },
    // 保存更新 或 新增员工
    async saveData() {
      this.$refs.userInfo.validate() // 表单校验
      let message = '新增员工成功'
      if (this.$route.params.id) {
        // 有用户id 则修改用户基本信息
        await uploadEmployeeInfo(this.$route.params.id, this.userInfo)
        message = '修改员工信息成功'
      } else {
        await addEmployee(this.userInfo)
      }
      this.$message.success(message)
      this.$router.replace('/employee') // 跳转页面 更新成功
    }
  }
}
</script>

<style scoped lang="scss">
.edit-form {
display: flex;
align-items: center;
justify-content: center;
  background: #fff;
  padding: 20px;
  margin-top: 20px;
  .inputW {
    width: 380px
  }
}
</style>
